package com.personal.interceptors;

import org.springframework.stereotype.Component;
import org.springframework.web.servlet.HandlerInterceptor;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

@Component
public class LoginInterceptor implements HandlerInterceptor {
    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
//        System.out.println("======拦截器======");
        HttpSession session = request.getSession();
        //将登录请求放行
        if (request.getRequestURI().contains("login")){
            return true;
        }
        //手机号验证请求放行
        if (request.getRequestURI().contains("checkName")){
            return true;
        }
        //注册请求放行
        if (request.getRequestURI().contains("register")){
            return true;
        }
        //判断是否登录，登录则放行
        if (session.getAttribute("logUser") != null){
            return true;
        }
        //没有登录则直接跳转登录界面
        request.getRequestDispatcher("/login.jsp").forward(request,response);
        return false;
    }
}
